import employees from '@/api/constant/employees'
import store from '@/store'
function changeStr(dom, obj) {
  const findItem = employees[obj.arg].find((item) => {
    return +item.id === +obj.value
  })
  dom.innerText = findItem ? findItem.value : '-'
}
let clickFn = ''
const directivesObj = {
  errorImg: {
    inserted(dom, obj) {
      dom.src = dom.src || obj.value || require('@/assets/common/head.jpg')
      dom.onerror = () => {
        dom.src = obj.value || require('@/assets/common/head.jpg')
      }
    },
    componentUpdated(dom, obj) {
      dom.src = dom.src || obj.value || require('@/assets/common/head.jpg')
    }
  },
  xxx: {},
  formatter: {
    /*
    bind,
    inserted
    update
    componentUpdated
    unbind
    */
    inserted(dom, obj, vnode) {
      // dom:指令所在dom
      // obj:{
      //   属性:arg
      //   修饰符
      //   值(value)
      // }
      // vnode:{context:指令所在组件的this}
      changeStr(dom, obj)
    },
    componentUpdated(dom, obj) {
      changeStr(dom, obj)
    }
  },
  // 点击dom区域外执行某事件
  clickOut: {
    inserted(dom, obj) {
      clickFn = (e) => {
        // dom:区域dom
        // 点击dom:e.target
        if (dom.contains(e.target)) {
          // console.log('区域内')
        } else {
          // console.log('区域外')
          obj.value()
        }
      }
      document.addEventListener('click', clickFn)
    },
    unbind() {
      document.removeEventListener('click', clickFn)
    }
  },
  buttonShow: {
    inserted(dom, obj) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    }
  }
}
// 循环对象
// for in
// Object.keys
// Object.values
// Object.entries

export default function(Vue) {
  Object.keys(directivesObj).forEach((key) => {
    Vue.directive(key, directivesObj[key])
  })
}
